import { Asset, Prefab } from "cc";
import { CommonUI } from "../../../[框架]/通用UI/CommonUI";
import { UIManager } from "../../../[框架]/通用UI/UIManager";
import TaskWeb from "../Controller/TaskWeb";

export default class TaskResMgr {

    public static ready: boolean = false; // 资源是否加载完成

    public static taskViewPrefab: Asset;

    public static destroy() {
        this.ready = false;
        this.taskViewPrefab = null;
        const uiManager = UIManager.getInstance();
        uiManager.destroy();
    }

    // 初始化资源，加载完成后 ready = tru

    public static async init(): Promise<boolean> {
        if (this.ready) {
            return true;
        }
        // TaskWeb.getTaskConfig();
        const uiManager = UIManager.getInstance();
        let res = await uiManager.init();
        if (!res) {
            Guyu.Log.error("TaskResMgr.init() 失败");
            return false;
        }
        return new Promise<boolean>((resolve) => {
            UIManager.instance.getBundle().load("任务/TaskView", Prefab, null, (err, prefab) => {
                if (err) {
                    Guyu.Log.error("任务/TaskView 预制体加载失败", err);
                    resolve(false);
                    return;
                }
                TaskResMgr.taskViewPrefab = prefab;
                Guyu.Log.info("任务/TaskView 预制体加载成功");
                TaskResMgr.ready = true;
                resolve(true);
            });

        });

    }
}